package ListQueue;

import java.util.NoSuchElementException;

public class DoubleLinkedList<E> {

    private int elementCount;
    private DLLNode<E> first;
    private DLLNode<E> last;

    public void addLast(E item) {
        last = new DLLNode<E>(item, null, last);
        if (first == null)
            first = last;
        elementCount++;
    }

    public boolean isEmpty() {
        return elementCount == 0;
    }

    public int size() {

        return elementCount;
    }

    public E getFirst() {
        if(isEmpty())  {
           throw new NoSuchElementException("List is empty");
        }
        return first.value();
    }

    public E removeFirst() {
        if (!isEmpty()) {
            DLLNode<E> temp = first;
            first = first.next();
            if (first != null) {
                first.setPrevious(null);
            } else {
                last = null;
            }
            temp.setNext(null);
            elementCount--;
            return temp.value();
        }
        return null;
    }

}

